package com.acoffee.java1;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * 需求1
 * @author acoffee
 * @create 2021-11-09 17:21
 */
public class BeanFactory {
    static Map<String,String> map = new HashMap<>();

   static  {
        Properties pros = new Properties();
        InputStream rs = BeanFactory.class.getResourceAsStream("ApplicationContext.properties");
        String perName = "";

       try {
           pros.load(rs);
           Set<Object> keys = pros.keySet();
           for (Object key : keys){
               perName = pros.getProperty(key+"");
               map.put(key+"",perName);
           }
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

   public Object getBean(String name){
       String className = map.get(name);
       Object obj = null;
       try {
           Class<?> aClass = Class.forName(className);
           obj = aClass.newInstance();
       } catch (Exception e) {

       }
       return obj;
   }

   @Test
   public void test1(){
       Person chinese = (Person) this.getBean("chinese");
       Person american = (Person) this.getBean("american");
       System.out.println(chinese);
       System.out.println(american);
   }
}
